package com.soft.idc.module.flight.control;

import java.util.List;

import org.eclipse.jetty.util.log.Log;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.CaptchaRender;
import com.soft.idc.common.control.AuthInterceptor;
import com.soft.idc.common.model.IdcUser;


@Before(AuthInterceptor.class)
public class FlightController extends Controller {

	public void index() {
		this.render("/map/map.html");
	}

	public void getGPSRecord() {
		String flightId = getPara("flight_id");
		String create_time = getPara("create_time");
		String sql = "select lng,lat,create_time from idc_flight_gps where flight_Id = ? ";
		List<Record> result = null;
		if (create_time != null && !"".equals(create_time)) {
			sql += "and create_time > str_to_date(?,'%Y-%m-%d %H:%i:%s')";
			sql += " order by create_time asc";
			result = Db.find(sql, flightId, create_time);
		} else {
			sql += " order by create_time asc";
			result = Db.find(sql, flightId);
		}

		this.renderJson(result);
	}

	public void loadAreaLable() {
		// String level = this.getPara("level");
		String sql = "select city,lng,lat,level from idc_airport_gps g";
		List<Record> result = Db.find(sql);
		this.renderJson(result);
	}

	public void loadFlightList() {
		String sql = "select i.*,(select concat(lng,'|',lat) from idc_airport_gps a where a.city = i.from_airport) as  from_gps,"
				+ "(select concat(lng,'|',lat) from idc_airport_gps a where a.city = i.to_airport) to_gps, "
				+ "(select CONCAT(lng,'|',lat) from idc_flight_gps g where g.flight_Id=i.flight_id order by g.create_time desc limit 0,1) as position "
				+ "from idc_flight_info i where status != ? ";
		IdcUser u = this.getSessionAttr("SYS_USER");
		List<Record> result = null;
		if (u.getOrgId() == 1) {
			result = Db.find(sql, 9);
		} else {
			sql += "and i.org_id = ?";
			result = Db.find(sql, 9, u.getOrgId());
		}
		this.renderJson(result);
	}

	public void getFlightInfo() {
		String flightId = this.getPara("flight_id");
		String sql = "select i.flight_code,from_airport,to_airport,fly_time,land_time,people,plan_land_time from idc_flight_info i where i.flight_id = ?";
		List<Record> list = Db.find(sql, flightId);
		if (list != null && list.size() > 0) {
			this.renderJson(list.get(0));
		} else {
			this.renderJson();
		}

	}

	public void getClearPlane() {
		String time = this.getPara("time");
		if (time == null || "".equals(time)) {
			time = "1";
		}
		IdcUser u = this.getSessionAttr("SYS_USER");

		String sql = "select flight_id from   idc_flight_info where status = ? ";
		List<Record> result = null;
		if (u.getOrgId() == 1) {
			result = Db.find(sql, 9);
		} else {
			sql += "and i.org_id = ?";
			result = Db.find(sql, 9, u.getOrgId());
		}
		this.renderJson(result);
	}

	public void getPlaneStatus() {
		String flightId = this.getPara("flight_id");
		IdcUser u = this.getSessionAttr("SYS_USER");
		String sql = "select flight_id,status from   idc_flight_info where flight_id = ? and status=9";
		List<Record> result = null;
		if (u.getOrgId() == 1) {
			result = Db.find(sql, flightId);
		} else {
			sql += "and i.org_id = ?";
			result = Db.find(sql, flightId, u.getOrgId());
		}
		if (result.size() > 0) {
			this.renderJson(result.get(0));
		} else {
			this.renderJson();
		}
	}
}
